# Get script directory and change to samples directory
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $scriptDir

# Initialize Emscripten environment
Write-Host "Initializing Emscripten environment..."
& "../../emsdk/emsdk_env.ps1"

# Check and create output directory
if (-not (Test-Path "dist")) {
    Write-Host "Creating dist directory..."
    New-Item -ItemType Directory -Name "dist" -Force
} else {
    Write-Host "dist directory already exists"
}

# Compile C++ code with emscripten
Write-Host "Compiling C++ code with Emscripten..."
em++ --bind -o dist/calculator.js src/calculator.cpp `
    -s MODULARIZE=1 `
    -s "ENVIRONMENT=node" `
    -s ALLOW_MEMORY_GROWTH=1 `
    -s EXPORT_NAME=Calculator `
    -O3

# Copy all files from dist directory to electron/dist/node directory
Write-Host "Copying all files from dist to electron/dist/node..."
if (-not (Test-Path "../../electron/dist/node")) {
    New-Item -ItemType Directory -Path "../../electron/dist/node" -Force

}
Copy-Item "dist/*" "../../electron/dist/node/" -Recurse -Force
